<?php
/**
 * Created by PhpStorm.
 * User: moon
 * Date: 2021/6/6
 * Time: 13:28
 */

namespace app\http\middleware;


use app\Request;
use think\facade\Cache;

class AdminCheck
{
    public function handle(Request $request, \Closure $next)
    {
        $token = $request->cookie('token', '');

        if (empty($token))
            return redirect('login');

        $admin = Cache::get($token);
        if (empty($admin))
            return redirect('login');

        $tokenCheck = sha1($_SERVER['REMOTE_ADDR']
            . $admin['account'] . $admin['passwd'] . $admin['admin_id'] . $admin['time']);

        if ($token != $tokenCheck)
            return redirect('login');
        Cache::set($token, $admin, 30 * 60);

        return $next($request);
    }
}
